package com.junxonline.instant.admin.controller.config;

import com.junxonline.instant.dao.entity.model.config.Setting;
import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.config.SettingVO;
import com.junxonline.instant.admin.service.config.SettingService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 参数管理Controller
 *
 * @author JunX
 * @date 2021-07-04
 */
@Log4j2
@RestController
@RequestMapping(value = "/config/setting")
public class SettingController {

    @Autowired
    private SettingService settingService;

    /**
     * 根据系统参数名称查询参数值
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresAuthentication
    @PostMapping(value = "/querySettingByName")
    public CommonResult querySettingByName(@RequestBody(required = false) SettingVO vo) {
        return CommonResult.success(settingService.querySettingByName(vo));
    }

    /**
     * 查询参数列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:setting:query")
    @OperationLog(label = "参数管理-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) CommonReqPage<SettingVO> vo) {
        return CommonResult.success(settingService.queryList(vo));
    }

    /**
     * 查询参数
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:setting:query")
    @OperationLog(label = "参数管理-查询参数", type = OperationEnum.QUERY)
    @PostMapping(value = "/query")
    public CommonResult query(@RequestBody(required = false) SettingVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(settingService.query(vo.getId()));
    }

    /**
     * 新增参数
     *
     * @param setting 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:setting:add")
    @OperationLog(label = "参数管理-新增参数", type = OperationEnum.ADD)
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody(required = false) Setting setting) {
        if (setting == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        settingService.add(setting);
        return CommonResult.success();
    }

    /**
     * 编辑参数
     *
     * @param setting 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:setting:edit")
    @OperationLog(label = "参数管理-编辑参数", type = OperationEnum.EDIT)
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody(required = false) Setting setting) {
        if (setting == null || setting.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        settingService.edit(setting);
        return CommonResult.success();
    }

    /**
     * 删除参数
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:setting:delete")
    @OperationLog(label = "参数管理-删除参数", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        settingService.delete(idsVO.getIds());
        return CommonResult.success();
    }

}
